var app=getApp();
var dateFormat = require('../../utils/dateutil');
var oneDay = 1*24*60*60*1000;
Page({
  data: {
    currentIndex: '0',
    startCity: '上海',
    endCity: '南京',
    hotelCity: '上海',
    adultNum: 1,
    childNum: 0,
    rotate: -180,
    toast1: {
      show: false,
      alertWarn: 'warn',
      info: ''
    },
    toast2: {
      show: false
    }
  },
  onLoad: function(){
    this.setData({
      FSdate: {
        date: dateFormat.formatTime(new Date(Date.now() + oneDay)),
        week: dateFormat.formatWeek(new Date(Date.now() + oneDay)),
        startday: dateFormat.formatDay(new Date(Date.now() + oneDay)),
        currentday: dateFormat.formatDay(new Date(Date.now() + oneDay))
      },
      FEdate: {
        date: dateFormat.formatTime(new Date(Date.now() + 2*oneDay)),
        week: dateFormat.formatWeek(new Date(Date.now() + 2*oneDay)),
        startday: dateFormat.formatDay(new Date(Date.now() + 2*oneDay)),
        currentday: dateFormat.formatDay(new Date(Date.now() + 2*oneDay))
      },
      HSdate: {
        date: dateFormat.formatTime(new Date(Date.now() + oneDay)),
        week: dateFormat.formatWeek(new Date(Date.now() + oneDay)),
        startday: dateFormat.formatDay(new Date(Date.now() + oneDay)),
        currentday: dateFormat.formatDay(new Date(Date.now() + oneDay))
      },
      HEdate: {
        date: dateFormat.formatTime(new Date(Date.now() + 2*oneDay)),
        week: dateFormat.formatWeek(new Date(Date.now() + 2*oneDay)),
        startday: dateFormat.formatDay(new Date(Date.now() + 2*oneDay)),
        currentday: dateFormat.formatDay(new Date(Date.now() + 2*oneDay))
      }
    })
  },


  ratioError: function(){
    var adultNum = this.data.adultNum;
    var childNum = this.data.childNum;
    return (adultNum*2)>=childNum;
  },

  rotate_img: function() {//旋转飞机图片
    if(this.data.currentIndex==1){
      var animation = wx.createAnimation({
        timingFunction: "ease",
        duration: 400
      })
      this.animation = animation;
      animation.rotateZ(this.data.rotate).step();

      this.setData({
        rotate: -1*this.data.rotate,
        startCity: this.data.endCity,
        endCity: this.data.startCity,
        animation: animation.export(),
      })

    }

  },
  

  changeBtn: function(ev) {//单程，往返切换
    if(ev.target.dataset.index==0){
      this.setData({
        rotate: 180,

      })
    }
    this.setData({
      currentIndex: ev.target.dataset.index
    })
  },

  bingDateChange: function(e){//绑定选择时间的函数
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case '1':
        this.setData({
          FSdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.FSdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          },
          FEdate: {
            date: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatTime(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatTime(new Date(this.data.FEdate.currentday)),
            week: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatWeek(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatWeek(new Date(this.data.FEdate.currentday)),
            startday: dateFormat.formatDay(new Date(new Date(e.detail.value).getTime() + oneDay)),//todo
            currentday: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatDay(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatDay(new Date(this.data.FEdate.currentday))
          },
          HSdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.HSdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          },
          HEdate: {
            date: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatTime(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatTime(new Date(this.data.FEdate.currentday)),
            week: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatWeek(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatWeek(new Date(this.data.FEdate.currentday)),
            startday: this.data.HEdate.startday,
            currentday: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatDay(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatDay(new Date(this.data.FEdate.currentday))
          }
        })
        break;
      case '2':
        this.setData({
          FEdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.FEdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          },
          HEdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.HEdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          }
        })
        break;
      case '3':
        this.setData({
          HSdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.HSdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          },
          HEdate: {
            date: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatTime(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatTime(new Date(this.data.FEdate.currentday)),
            week: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatWeek(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatWeek(new Date(this.data.FEdate.currentday)),
            startday: dateFormat.formatDay(new Date(new Date(e.detail.value).getTime() + oneDay)),//todo
            currentday: this.compareDay(e.detail.value, this.data.FEdate.currentday) === false ? dateFormat.formatDay(new Date(new Date(e.detail.value).getTime() + oneDay)) : dateFormat.formatDay(new Date(this.data.FEdate.currentday))
          }
        })
        break;
      case '4':
        this.setData({
          HEdate: {
            date: dateFormat.formatTime(new Date(e.detail.value)),
            week: dateFormat.formatWeek(new Date(e.detail.value)),
            startday: this.data.HEdate.startday,
            currentday: dateFormat.formatDay(new Date(e.detail.value))
          }
        })
        break;
    }
  },

  compareDay: function(startday,endday){
    var startSecond = new Date(startday).getTime();
    var endSecond = new Date(endday).getTime();
    if((endSecond - startSecond) > oneDay){
      return true;
    }else{
      return false;
    }
  },

  alertWarn: function(){
    var obj = {
      pointer: this,
      duration: 3000
    }
    app.toast2(obj);
  },
  searchProduct: function(){//跳转详情页
    console.info(this.data)
    let startCity=this.data.startCity
    let endCity=this.data.endCity
    let startDate=  dateFormat.formatDay2(new Date(this.data.FSdate.currentday))
    let endDate=  dateFormat.formatDay2(new Date(this.data.FEdate.currentday))
    if(this.data.currentIndex=='0'){
      endDate=startDate
    }
    wx.navigateTo({
      url:`../productDetail/productDetail?START_CITY=${startCity}&END_CITY=${endCity}&START_DATE=${startDate}&END_DATE=${endDate}&type=${this.data.currentIndex}`
    })
  },
  selectCity: function(e){//选择城市 切换保存方法
    var type = e.currentTarget.dataset.type;
    var that = this;
    console.info('type',type)
    var url=type!=1?'../selectStartCity/selectStartCity':'../selectStartCity/selectStartCity';
    url=url+'?type='+type;
    app.globalData.cityFn = function(city,type){
      switch (type) {
        case '1':
          that.setData({
            startCity: city
          });
          break;
        case '2':
          that.setData({
            endCity: city,
            hotelCity: city
          });
          break;
        case '3':
          that.setData({
            hotelCity: city
          });
          break;
      }
    };

    wx.navigateTo({
      url
    })
  }
})
